package com.fileoperation.user;


import android.content.Context;

import com.fileoperation.model.SaveUserInfoEvent;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/10/23.
 */

public class UserInfoParser {
    public static void parseUserInfo(JSONObject data, Context context) {
        if (data != null) {
            UserInfo.getInstance().setAccessToken(data.optString(UserInfo.ACCESS_TOKEN, null));
            UserInfo.getInstance().setRefreshToken(data.optString(UserInfo.REFRESH_TOKEN, null));
            JSONObject user = data.optJSONObject("user");
            if (user != null) {
                UserInfo.getInstance().setUuid(user.optString(UserInfo.UUID, null));
                UserInfo.getInstance().setUsername(user.optString(UserInfo.USER_NAME, null));
            }
            UserInfo.getInstance().saveUserData(context);
        }

    }


    public static void parseUserInfo(JSONObject data) {

        if (data != null) {
            UserInfo.getInstance().setAccessToken(data.optString(UserInfo.ACCESS_TOKEN, null));
            UserInfo.getInstance().setRefreshToken(data.optString(UserInfo.REFRESH_TOKEN, null));
            JSONObject user = data.optJSONObject("user");
            if (user != null) {
                UserInfo.getInstance().setUuid(user.optString(UserInfo.UUID, null));
                UserInfo.getInstance().setUsername(user.optString(UserInfo.USER_NAME, null));
            }
            EventBus.getDefault().postSticky(new SaveUserInfoEvent());
            System.out.println("发出保存用户信息事件");
        }

    }
}
